package com.jing.ads.services;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.text.TextUtils;
import android.widget.Toast;

import com.jing.ads.log.Logger;
import com.jing.ads.util.AlarmUtils;
import com.jing.ads.util.GlobalUtil;
import com.jing.ads.util.PreferenceUtil;

public class QueueService extends Service {

	public static final String KEY_GET = "key_get";
	public static final String KEY_SEND = "key_send";
	public static final String KEY_FIRST = "key_first";

	public static final String KEY_GET_QUEUE = "key_get_queue";
	public static final String KEY_SEND_QUEUE = "key_send_queue";
	
	public static final String ACTION_QUEUE = "com.jing.ads.service.action.queue";
	
	private MyReceiver receiver;
	
	class MyReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (ACTION_QUEUE.equals(action)) {
				if (Logger.isDebug) {
//					Toast.makeText(context, "接收启动轮询广播", Toast.LENGTH_SHORT).show();
				}
				if(!checkFirstLaunch()){
					onQueue();
				}
//				kill(2000);
			}
		}
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		if (Logger.isDebug) {
//			Toast.makeText(this, "轮询服务启动", Toast.LENGTH_SHORT).show();
		}
		receiver = new MyReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(ACTION_QUEUE);
		this.registerReceiver(receiver, filter);
	}

	@Override
	public void onDestroy() {
		if (Logger.isDebug) {
//			Toast.makeText(this, "轮询服务关闭", Toast.LENGTH_SHORT).show();
		}
		super.onDestroy();
		if (receiver != null) {
			this.unregisterReceiver(receiver);
		}
	}

	private boolean checkFirstLaunch() {
		boolean first = false;
		String f = PreferenceUtil.getSharedString(this, KEY_SEND, "");
		if (TextUtils.isEmpty(f)) {
			AlarmUtils.launchAlarmForQueue(this, true);
			startNetService();
			PreferenceUtil.putSharedLong(this, KEY_SEND_QUEUE, System.currentTimeMillis());
			TimerTask tt = new TimerTask() {

				@Override
				public void run() {
					Intent i = new Intent(NetworkService.ACTION_DATA_SEND);
					sendBroadcast(i);
				}
			};
			PreferenceUtil.putSharedLong(this, KEY_GET_QUEUE, System.currentTimeMillis());
//			TimerTask ttt = new TimerTask() {
//
//				@Override
//				public void run() {
//					Intent i = new Intent(NetworkService.ACTION_DATA_GET);
//					sendBroadcast(i);
//				}
//			};
			Timer t = new Timer();
			t.schedule(tt, 2000);
//			Timer gt = new Timer();
//			gt.schedule(ttt, 2000);
			first = true;
		}
		return first;
	}

	private void kill(long delay) {
		TimerTask close = new TimerTask() {
			
			@Override
			public void run() {
				QueueService.this.stopSelf();
			}
		};
		Timer c = new Timer();
		c.schedule(close, delay);
	}
	
	private void startNetService() {
		if (!GlobalUtil.isServiceRunning(this, NetworkService.class.getName())) {
			Intent i = new Intent(this, NetworkService.class);
			startService(i);
		}
	}

	private void onQueue() {
		if (Logger.isDebug) {
//			Toast.makeText(this, "启动轮询检测 ", 400).show();
		}
		String send = PreferenceUtil.getSharedString(this, KEY_SEND, "");
		if (!TextUtils.isEmpty(send)) {
			startNetService();
			long sq = PreferenceUtil.getSharedLong(this, KEY_SEND_QUEUE, -1);
			if (sq == -1 || GlobalUtil.isInQueue(send, sq)) {
				if (Logger.isDebug) {
//					Toast.makeText(this, "SendApps轮询检测 = yes " + sq, Toast.LENGTH_SHORT).show();
				}
				PreferenceUtil.putSharedLong(this, KEY_SEND_QUEUE, System.currentTimeMillis());
				TimerTask tt = new TimerTask() {

					@Override
					public void run() {
						Intent i = new Intent(NetworkService.ACTION_DATA_SEND);
						sendBroadcast(i);
					}
				};
				Timer t = new Timer();
				t.schedule(tt, 2000);
			}else {
				if (Logger.isDebug) {
//					Toast.makeText(this, "SendApps轮询检测 = no " + sq, Toast.LENGTH_SHORT).show();
				}
			}
		}
		String get = PreferenceUtil.getSharedString(this, KEY_GET, "");
		if (!TextUtils.isEmpty(get)) {
			startNetService();
			long sq = PreferenceUtil.getSharedLong(this, KEY_GET_QUEUE, -1);
			if (sq == -1 || GlobalUtil.isInQueue(get, sq)) {
				if (Logger.isDebug) {
//					Toast.makeText(this, "GetApps轮询检测 = yes" + sq, Toast.LENGTH_SHORT).show();
				}
				PreferenceUtil.putSharedLong(this, KEY_GET_QUEUE, System.currentTimeMillis());
				TimerTask ttt = new TimerTask() {

					@Override
					public void run() {
						Intent i = new Intent(NetworkService.ACTION_DATA_GET);
						sendBroadcast(i);
					}
				};

				Timer gt = new Timer();
				gt.schedule(ttt, 2000);
			}else {
				if (Logger.isDebug) {
//					Toast.makeText(this, "GetApps轮询检测 = no " + sq, Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

}
